<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">角色名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="rolename" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">角色编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="roleno" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">角色备注</label>
                            <div class="layui-input-inline">
                                <input type="text" name="remark" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 保存 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/jquery-1.12.1.min.js"></script>
<script src="/js/common.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        var url = decodeURI(window.location.href);
        var argsIndex = url.split("?obj=");
        var arg = argsIndex[1];

        //用户信息
        let userModel=api.getUserModelByUid(arg);
        //用户角色id列表
        let userRoleModelIds=api.getRoleUserModelRoleIdsByUid(arg);
        var array=userRoleModelIds.data;

        table.render({
            elem: '#currentTableId',
            method: 'post', //如果无需自定义HTTP类型，可不加该参数
            url: '/role/page',
            toolbar: '#toolbarDemo',
            contentType: 'application/json',
            request: {
                pageName: 'pageNum' //页码的参数名称，默认：page
                ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            headers: {
                'Token':localStorage.getItem('Token')
            } ,//如果无需自定义请求参数，可不加该参数
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'rolename', width: 150, title: '角色名称'},
                {field: 'roleno', width: 150, title: '角色编号'},
                {field: 'remark', title: '角色备注', width: 200},
                {field: 'createdate', width: 200, title: '创建时间', sort: true},
                {
                    field: 'id', width: 200, align: 'center', templet: function (d) {
                        if (array.includes(d.id)) {
                            return'      <input type="checkbox" checked="" name="close" value='+d.id+' lay-skin="switch"  lay-filter="switchTest" lay-text="ON|OFF">\n' ;
                        }else {
                            return'      <input type="checkbox"  name="close" lay-skin="switch" value='+d.id+'  lay-filter="switchTest" lay-text="ON|OFF">\n' ;
                        }
                    }, title: '授权'
                },
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: data.field
                }
            }, 'data');

            return false;
        });

        Array.prototype.remove = function (val) {
            var index = this.indexOf(val);
            if(index > -1){this.splice(index,1);}
        }

        form.on('switch(switchTest)', function(obj){
            if(obj.elem.checked){
                let num=parseInt(this.value)
                array.push(num)
                console.log("方入"+JSON.stringify(array))
            }else {
                let num=parseInt(this.value)
                array.remove(num);
                console.log("移除"+JSON.stringify(array))
            }
        });


        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                let res={};
                res.uid=parseInt(arg);
                res.roleIds=array;
                let result=api.saveRoleuserModel(res)
                layer.msg(result.msg)
            } else if (obj.event === 'delete') {  // 监听删除操作

            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../../page/role/updaterole.html?obj='+data.id,
                    end:function(){
                        table.reload('currentTableId')
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            }else if(obj.event==='role'){

                var index = layer.open({
                    title: '角色菜单授权',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../../page/role/addrolemenu.html?obj='+data.id,
                    end:function(){
                        table.reload('currentTableId')
                    }
                });
            }
        });

    });
</script>

</body>
</html>